package com.sdvdxl.action;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import com.sdvdxl.BlogNotFoundException;
import com.sdvdxl.model.BlogEntity;
import com.sdvdxl.model.BlogTagEntity;
import com.sdvdxl.model.BlogTypeEntity;
import com.sdvdxl.model.PaggerEntity;
import com.sdvdxl.service.BlogService;
import com.sdvdxl.service.BlogTypeService;
import com.sdvdxl.util.BlogHandler;
import com.sdvdxl.util.GlobalVars;
import com.sdvdxl.util.JsonResult;
import com.sdvdxl.util.LoggerUtil;

public class BlogAction extends BaseAction {
	private BlogEntity blogEntity;
	private BlogService blogService;
	private BlogTypeService blogTypeService;
	private String action;
	private BlogTagEntity blogTagEntity;
	private BlogTypeEntity blogTypeEntity;
	private PaggerEntity<BlogEntity> paggerEntity;
	public PaggerEntity<BlogEntity> getPaggerEntity() {
		return paggerEntity;
	}

	public void setPaggerEntity(PaggerEntity<BlogEntity> paggerEntity) {
		this.paggerEntity = paggerEntity;
	}

	private static LoggerUtil logger = new LoggerUtil(BlogAction.class);

	public BlogTagEntity getBlogTagEntity() {
		return blogTagEntity;
	}

	public void setBlogTagEntity(BlogTagEntity blogTagEntity) {
		this.blogTagEntity = blogTagEntity;
	}

	public BlogTypeEntity getBlogTypeEntity() {
		return blogTypeEntity;
	}

	public void setBlogTypeEntity(BlogTypeEntity blogTypeEntity) {
		this.blogTypeEntity = blogTypeEntity;
	}

	public BlogTypeService getBlogTypeService() {
		return blogTypeService;
	}

	public void setBlogTypeService(BlogTypeService blogTypeService) {
		this.blogTypeService = blogTypeService;
	}

	public BlogEntity getBlogEntity() {
		return blogEntity;
	}

	public void setBlogEntity(BlogEntity blogEntity) {
		this.blogEntity = blogEntity;
	}

	public BlogService getBlogService() {
		return blogService;
	}

	public void setBlogService(BlogService blogService) {
		this.blogService = blogService;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	/**
	 * 添加博文，如果action是new，则跳转到添加页面，如果是add，则执行添加博文代码
	 * 
	 * @return 跳转页面动作代码
	 */
	public String addBlog() {
		if ("add".equals(this.action)) {
			JsonResult result = new JsonResult();
			try {
				result = blogService.add(blogEntity);
			} catch (Exception e) {
				logger.error(e);
			}
			writeJson(result);
			return null;
		} else {
			List<BlogTypeEntity> blogTypeList = blogTypeService.getAll();
			request.setAttribute("blogTypeList", blogTypeList);
			return INPUT;
		}
	}

	/**
	 * 获取无分页的所有博文
	 * 
	 * @return
	 */
	public String getAllBlogs() {
		List<BlogEntity> blogs = blogService.getAll();
		request.setAttribute("allBlogs", blogs);
		return "getAllBlogs";
	}

	/**
	 * 博文详情
	 * 
	 * @return
	 */
	public String getDeteal() throws BlogNotFoundException {
		try {
			BlogEntity blog = blogService.get(blogEntity);
			blog = new BlogHandler(blog).readBlog();// 读取正文
			request.setAttribute("blogDeteal", blog);
		} catch (FileNotFoundException e) {
			request.setAttribute("blogMessage", "此博文不存在");
			logger.error("博文不存在，路径：" + GlobalVars.getInstance().getBlogPath()
					+ "/" + blogEntity.getPath() + "/" + blogEntity.getBlogId());
		} catch (IOException e) {
			logger.error("读取博文数据流异常");
			logger.error(e);
			request.setAttribute("blogMessage", "获取博文失败");
		} catch (Exception e) {
			logger.error(e);
			request.setAttribute("blogMessage", "未知错误");
		}

		return "deteal";
	}

	/**
	 * 根据标签加载博文
	 * 
	 * @return 转发页面
	 */
	public String getBlogsByTag() {
		List<BlogEntity> blogs = blogService.getBlogsByTag(blogTagEntity);
		request.setAttribute("allBlogs", blogs);
		return "getAllBlogs";
	}

	/**
	 * 根据分类加载博文
	 * 
	 * @return 转发页面
	 */
	public String getBlogsByType() {
		List<BlogEntity> blogs = blogService.getBlogsByType(blogTypeEntity);
		request.setAttribute("allBlogs", blogs);
		return "getAllBlogs";
	}

	/**
	 * 取得分页后的博文
	 * 
	 * @return
	 */
	public String getBlogsByTypeWithPagger() {
		List<BlogEntity> blogs = blogService.getBlogsByTypeWithPagger(paggerEntity);
		paggerEntity.setTargetEntities(blogs);
		request.setAttribute("allBlogs", blogs);
		return "getAllBlogs";
	}
}
